package com.ylw.entity.define;

import com.ylw.entity.Define;
import com.ylw.entity.Service;
import com.ylw.util.Constant;
import com.ylw.util.XWPFTableUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.apache.poi.xwpf.usermodel.XWPFTable;

import java.util.ArrayList;
import java.util.List;

/**
 * 使用的是First.docx里面的模板
 */
public class FirstDefine extends Define {
    protected static final String SERVICE_DEPENDENCY = "服务依赖";
    protected static final List parameter = new ArrayList();
    private static final Logger logger = Logger.getLogger(FirstDefine.class);

    static {
        parameter.addAll(superParament);
        parameter.add(SERVICE_DEPENDENCY);
    }

    protected List<ServiceDependency> serviceDependencies = new ArrayList();//服务依赖


    public FirstDefine(Service service,XWPFTable table) {
        super(service,table);
    }


    @Override
    public boolean check(XWPFTable table) {
        return true;
    }

    @Override
    public String read() {
        if (table == null) {
            return Constant.NULL;
        }
        XWPFTableRow row = null;
        for (int i = 0; i < table.getNumberOfRows(); i++) {
            row = table.getRow(i);
            if (SERVICE_DEPENDENCY.equalsIgnoreCase(row.getCell(0).getText().trim())) {
                solveDependency(i, 0);
                break;
            }
            readKeyValue(row, 0, parameter.get(i * 2).toString());
            readKeyValue(row, 2, parameter.get(i * 2 + 1).toString());
        }
        return Constant.FINISHED;
    }




    /**
     * 处理服务依赖这个表格
     * @param row
     * @param col
     * @return 返回服务依赖表格结束行
     */
    private int solveDependency(int row, int col) {
        String key = table.getRow(row).getCell(col).getText().trim();
        int spanNum = 0 ;
        if (SERVICE_DEPENDENCY.equalsIgnoreCase(key)) {
            spanNum = XWPFTableUtil.getRowspan(table, row, col);
            for (int i = 0; i < spanNum; i++) {
                XWPFTableRow tr = table.getRow(i + row);
                String serviceCode = tr.getCell(col + 1).getText().trim();
                String serviceName = tr.getCell(col + 2).getText().trim();
                if (StringUtils.isBlank(serviceCode) || StringUtils.isBlank(serviceName)) {
                    logger.error(service.getName() + " is blank " + serviceCode + serviceName);
                }
                else {
                    serviceDependencies.add(new ServiceDependency(serviceCode, serviceName));
                }
            }
            map.put(SERVICE_DEPENDENCY, serviceDependencies);
        }
        else {
            logger.error(service.getName() + " " + SERVICE_DEPENDENCY + Constant.NOT_MATCH + key);
        }
        return row+spanNum;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("{");
        sb.append("\"serviceDependencies\":").append(serviceDependencies);
        sb.append(",\"table\":").append(table);
        sb.append(",\"map\":").append(map);
        sb.append(",\"service\":").append(service);
        sb.append('}');
        return sb.toString();
    }
}
